package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.*;
import com.cqjtu.eecs.entity.vo.GraduateRequirementDetailVo;
import com.cqjtu.eecs.entity.vo.RadarGraphVo;
import com.cqjtu.eecs.entity.vo.RequirementVO;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.mapper.*;
import com.cqjtu.eecs.service.IRequirementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 毕业要求 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@Service
public class RequirementServiceImpl extends ServiceImpl<RequirementMapper, Requirement> implements IRequirementService {

    @Autowired
    private RequirementMapper requirementMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private CourseGoalMapper courseGoalMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private NaturalClassMapper naturalClassMapper;

    @Autowired
    private CourseGoalScoreMapper courseGoalScoreMapper;


    /**
     * 根据培养方案编号查询毕业要求
     * @param pid
     * @return
     */
    @Override
    public List<RequirementVO> getRequirement(String pid) {
        try {
            List<RequirementVO> requirementVos = requirementMapper.getRequirementByPid(pid);
            return requirementVos;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取自然班的毕业要求雷达图数据
     * @param className
     * @param grade
     * @param majorName
     * @return
     */
    @Override
    public List<RadarGraphVo> getRadarGraphData(String className, Integer grade, String majorName) {
        Major major = majorMapper.selectOne(new QueryWrapper<Major>().eq("major_name", majorName));
        if (major == null)
            throw new NotFoundException("专业" + majorName + "不存在");
        // 自然班
        NaturalClass naturalClass = naturalClassMapper.selectOne(new QueryWrapper<NaturalClass>().eq("grade", grade)
                .eq("major_code", major.getMajorCode()).eq("natural_class_name", className));
        if (naturalClass == null)
            throw new NotFoundException("自然班" + className + "不存在");
        return this.getRadarGraphVoList(major.getMajorCode(), naturalClass.getNcId(), grade);
    }

    public List<RadarGraphVo> getRadarGraphVoList(String majorCode, Integer ncId, Integer grade) {
        try {
            // 获取自然班的学生人数
            Integer stuNum = studentMapper.selectCount(new QueryWrapper<Student>().eq("nc_id", ncId));

            // 获取该专业所有毕业要求二级指标点
            List<Requirement> requirementList = this.getRequirementListByMajorNameAndGrade(majorCode, grade);

            List<RadarGraphVo> radarGraphVoList = new ArrayList<>();
            // 遍历每个毕业要求二级指标点
            requirementList.forEach(requirement -> {
                RadarGraphVo radarGraphVo = new RadarGraphVo();
                radarGraphVo.setName("B" + requirement.getRequirementCode());

                // 支撑毕业要求的所有课程目标
                List<CourseGoal> courseGoalList = courseGoalMapper.selectList(new QueryWrapper<CourseGoal>().eq("requirement_id", requirement.getRequirementId()));

                float achieveValue = 0f;
                float reachValue = 0f;
                for (CourseGoal courseGoal : courseGoalList) {
                    Double contributionSum = courseGoalScoreMapper.getContributionSumOfNaturalClass(courseGoal.getCourseGoalId(), ncId);
                    if (contributionSum != null) {
                        achieveValue += courseGoal.getWeight();
                        reachValue += (contributionSum / stuNum);
                    }
                }
                radarGraphVo.setAchieveValue(String.format("%.1f", achieveValue * 100));
                radarGraphVo.setReachValue(String.format("%.1f", reachValue * 100));

                float degree;
                if (achieveValue == 0)
                    degree = 0;
                else
                    degree = (reachValue / achieveValue);
                radarGraphVo.setDegree(String.format("%.1f", degree * 100));

                radarGraphVoList.add(radarGraphVo);
            });
            return radarGraphVoList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 通过年级专业名查询毕业要求二级指标点
    public List<Requirement> getRequirementListByMajorNameAndGrade(String majorCode, Integer grade) {

        Project project = projectMapper.selectOne(new QueryWrapper<Project>().eq("major_code", majorCode)
                .eq("grade", grade));
        if (project == null)
            throw new NotFoundException("专业培养方案不存在");

        return requirementMapper.selectList(new QueryWrapper<Requirement>().eq("pid", project.getPid())
                .isNotNull("pre_requirement_id"));
    }
}
